package mse.eai.esb;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;



import messageObjects.BalanceObj;
import messageObjects.MovementObj;

import org.jboss.logging.Logger;
import org.jboss.soa.esb.actions.ActionLifecycle;
import org.jboss.soa.esb.actions.ActionLifecycleException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ParseBalance implements ActionLifecycle {

	private static final Logger log = Logger.getLogger(ParseBalance.class);
	
	@Override
	public void destroy() throws ActionLifecycleException {
		// TODO Auto-generated method stub

	}

	@Override
	public void initialise() throws ActionLifecycleException {
		// TODO Auto-generated method stub

	}

	public ParseBalance(ConfigTree config) {

	}

	public Message process(Message m) {

		
		
		try {
			
			
			String xml = (String) m.getBody().get("org.jboss.soa.esb.message.defaultEntry");
			
			InputSource inputSource = new InputSource(new StringReader(xml));

			XPath xPath = XPathFactory.newInstance().newXPath();

			// XPathExpression
			// xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
			// String title = xPathExpression.evaluate(inputSource);

			String expression = "//return";
			NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource,
					XPathConstants.NODESET);
			
			
			String balance = "";
			List<MovementObj> movements = new ArrayList<MovementObj>();


			System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&& "+ xml);
			
			for (int i = 0; i < nodes.getLength(); i++) {
			
				Node node = nodes.item(i);
				Node balanceNode = (Node) xPath.evaluate("lindenEuros",
						node, XPathConstants.NODE);
				
				System.out.println("NAME: "+balanceNode.getNodeName());
				System.out.println("VALUE: "+balanceNode.getTextContent());	
				
				
				balance = balanceNode.getTextContent();
				
				System.out.println("BALANCE: "+ balance);

				
				NodeList nodesMovements = (NodeList) xPath.evaluate("movements", node,
						XPathConstants.NODESET);
				
				System.out.println("HOW MANY MOVMENTS: "+nodesMovements.getLength());

				
				for (int j = 0; i < nodesMovements.getLength(); i++) {
					Node nodeMovement = nodesMovements.item(i);
					
					System.out.println("NAME: "+nodeMovement.getNodeName());
					System.out.println("VALUE: "+nodeMovement.getTextContent());	
					
					Node dateNode = (Node) xPath.evaluate("date",
							nodeMovement, XPathConstants.NODE);
					Node amountNode = (Node) xPath.evaluate("lindenEuros",
							nodeMovement, XPathConstants.NODE);
					Node amountType = (Node) xPath.evaluate("lindenmovementstype",
							nodeMovement, XPathConstants.NODE);
					
					Node moveDescNode = (Node) xPath.evaluate("description",
							amountType, XPathConstants.NODE);			
					
					MovementObj mov = new MovementObj(dateNode.getTextContent(), moveDescNode.getTextContent(), amountNode.getTextContent());
					movements.add(mov);
					
				}	
				
				
				
			}	
		
			
			
			BalanceObj b = new BalanceObj(balance,movements);
			m.getBody().add("balance",b);
			
			System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&");

			

		return m;
		
		} catch(Exception e){ e.printStackTrace(); return m; }
	}
	
}
